﻿using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Timers;
using System.Web;
using System.Web.Mail;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace Halide
{
	/// <summary>
	/// The ClientSide class contains methods and properties for manipulating the client-side markup of a page.
	/// </summary>
	sealed public class ClientSide
	{
		private static int counter;
		private static string _version = "2008.07.09A";

		/// <summary>
		/// Return the version number of the class; read-only.
		/// </summary>
		/// <value>
		/// A string with the version number of the class.
		/// </value>
		public static string Version
		{
			get
			{
				return _version;
			}
		}

		/// <summary>
		/// No constructor is necessary.
		/// </summary>
		private ClientSide()
	    {
	    }

		/// <summary>
		/// Insert a script block which creates a JSON object with an array of
		/// client control ID names for all server-side controls. This allows access
		/// to the objects using javascript. It is best used in the Page_PreRender block.
		/// <example>
		/// <code>
		///		Halide.ClientSide.CreateJsonIDArray(Page, "en");
		/// </code>
		/// Assuming that there is a server-side control on the page, with the ID
		/// "Control01", its run-time ID is something cryptic, like "ctl00_Body_Control01".
		/// After the code above is executed, however, jQuery code on the page itself can refer
		/// to all server-side controls by their server-side names, as:
		/// <code>
		///		$(en.Control01).css('color', 'Red');
		/// </code>
		/// </example>
		/// </summary>
		/// <param name="page">The Page object.</param>
		/// <param name="objectName">Name for the object with the array of names.</param>
		public static void CreateJsonIDArray(Page page, string objectName)
		{
			if (objectName.Contains(" "))
			{
				throw new Exception("CreateJsonIDArray objectName cannot contain spaces");
			}

			else
			{
				counter = 0;
				string script = en(page);
				page.ClientScript.RegisterClientScriptBlock(page.GetType(), objectName, objectName + "={" + script + "};", true);
			}
		}

		private static string en(Control p)
		{
			string retVal = "";

			foreach (Control c in p.Controls)
			{
				if (!String.IsNullOrEmpty(c.ID))
				{
					retVal +=
						(counter > 0 ? "," : "")
						+ c.ID
						+ ":\"#"
						+ c.ClientID
						+ "\"";

					counter++;
				}

				retVal += en(c);
			}

			return (retVal);
		}
	}
}
